package com.itheima.reggie.controller;


import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 后台系统登录功能
     * @param employee
     * @return
     */
    @RequestMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session){
        //调用业务层完成登录
        Employee loginEmployee = employeeService.login(employee);
        Integer code=loginEmployee!=null?1:0;
        String msg=loginEmployee!=null?"":"用户名或密码不可用";
        R<Employee> employeeR = new R<>();
        employeeR.setCode(code);
        employeeR.setMsg(msg);
        employeeR.setData(loginEmployee);
        //存储登录用户到session
        session.setAttribute("employee",loginEmployee.getId());

        return employeeR;

    }

    /**
     * 后台系统退出功能
     * @param session
     */
    @RequestMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();
        R<Object> objectR = new R<>();
        objectR.setCode(1);
        return objectR;
    }

    /**
     * 新增员工
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R addEmployee(@RequestBody Employee employee,HttpSession session){
        Employee loginEmployee = (Employee) session.getAttribute("loginEmployee");
        Long id = loginEmployee.getId();
        //调用service完成添加
        employee.setPassword("e10adc3949ba59abbe56e057f20f883e");
        employee.setStatus(1);
        employee.setCreateUser(id);
        employee.setUpdateUser(id);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Boolean flag = employeeService.addEmployee(employee);
        Integer code=flag?1:0;
        String msg=flag?"添加成功":"添加失败";
        R<Object> R = new R<>();
        R.setCode(code);
        R.setMsg(msg);
        return R;
    }

    /**
     * 分页查询员工
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findAllByPage(@RequestParam("page") Long page,@RequestParam("pageSize")Long pageSize,@RequestParam(value = "name",required = false)String name){
        Page pageBean = employeeService.findByPage(name, page, pageSize);
        R<Page> pageR = new R<>();
        pageR.setCode(1);
        pageR.setData(pageBean);
        return pageR;
    }

    /**
     * 数据回显，根据id查询员工
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public R selectById(@PathVariable Long id){
        Employee employee = employeeService.selectById(id);
        R<Employee> employeeR = new R<>();
        employeeR.setData(employee);
        employeeR.setCode(1);
        return employeeR;
    }


    @PutMapping
    public R updateEmployee(@RequestBody Employee employee){
        Boolean flag= employeeService.update(employee);
        Integer code=flag?1:0;
        String msg=flag?"修改成功":"修改失败";
        R<Object> R = new R<>();
        R.setCode(code);
        R.setMsg(msg);
        return R;
    }
}
